package logic

import (
	"activity_srv/activity"
	"activity_srv/internal/dao"
	"activity_srv/internal/svc"
	"context"
	"errors"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type JoinGroupLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewJoinGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *JoinGroupLogic {
	return &JoinGroupLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *JoinGroupLogic) JoinGroup(in *activity.JoinGroupRequest) (*activity.JoinGroupResponse, error) {
	// 调用DAO层的方法参与拼团
	group, err := dao.JoinGroup(in.InviteCode, in.UserId, in.ProductNum, in.UserOrderSn)
	if err != nil {
		fmt.Println("参与拼团失败:", err)
		return nil, errors.New("参与拼团失败: " + err.Error())
	}

	return &activity.JoinGroupResponse{
		Message:     "参与拼团成功",
		GroupId:     int64(group.Id),
		GroupSn:     group.GroupSn,
		UserOrderSn: group.UserOrderSn,
	}, nil
}
